<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ page session="true"%>
<html>
<head>
<meta charset="UTF-8">
<title>后台管理</title>
<script src="./js/jQuery.js"></script>
<link
	href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css"
	rel="stylesheet">
</head>
<body>
	<div class="alert" id="alert"
		style="position: absolute; width: 100%; padding: 0px"></div>
	<%@ include file="/html/head.jsp"%>

	<br>
	<br>
	<div class="container">
		<div class="masthead">
			<form method="post">
				<table border="1" width="100%" class="table table-striped">
					<tr>
						<!-- <th><input type="checkbox" id="chbAll"></th> -->
						<th>ID</th>
						<th>标题</th>
						<th>摘要</th>
						<th>出版日期</th>
						<th>操作</th>
					</tr>
					<tbody id="tableBody">
						<c:forEach var="NewsInfo" items="${NewsInfos}">
							<tr>
								<!-- <th><input type="checkbox" name="ids" value="${NewsInfo.id}"  ></th> -->
								<td style="vertical-align: middle;">${NewsInfo.id}</td>
								<td style="vertical-align: middle;">${NewsInfo.head}</td>
								<td style="vertical-align: middle;">${NewsInfo.body}</td>
								<td style="vertical-align: middle;"><fmt:formatDate
										value="${NewsInfo.date}" pattern="yyyy年MM月dd日" /></td>
								<td style="vertical-align: middle;"><a href="#"
									class="btn  btn-primary btn-read-news">查看</a> <a href="#"
									class="btn btn-success btn-update-news">编辑</a> <a href="#"
									class="btn btn-danger btn-delete-news">删除</a></td>
							</tr>
						</c:forEach>
					</tbody>

				</table>
			</form>
		</div>
	</div>
	<script type="text/javascript">
		var _code = '${code}';
		if (_code == 200) {
			alertDelay(1,"删除成功");
		}
		$(".btn-read-news").click(function() {
			var newsId = $(this).parent().parent().find("td:eq(0)").text();
			var newSTitle = $(this).parent().parent().find("td:eq(1)").text();
			alert("查看新闻的ID" + newsId);
		})
		$(".btn-update-news").click(function() {
			var newsId = $(this).parent().parent().find("td:eq(0)").text();
			alert("编辑新闻的ID" + newsId);

		})
		$(".btn-delete-news").click(function() {
				var newsId = $(this).parent().parent().find("td:eq(0)").text();
				$.ajax({
						url : "/News/deleteNews.html",
						type : "GET",
						data : {
							id:newsId
						},
						dataType : "json",
						cache : false,
						processData : false,
						success : function(data) {
							if (data.state == 200) {
								window.location.href = '/News/backManagent.html?code='+data.state
							} else if (data.state == 101) {
								alertDelay(4, '删除失败');
							}
						},
						error : function(err) {
							alertDelay(4, '删除失败');
						}
					});
		})
		function alertDelay(state, message, time) {
			$('#alert').stop();
			if (time == null) {
				time = 1500;
			}
			switch (state) {
			case 1:
				var success = createRandomId();
				$('<div>').appendTo('#alert').attr('id', success).css("width",
						"100%").addClass('alert alert-success').html(message)
						.show().delay(time).fadeOut(function() {
							$("#" + success).remove();
						});
				break;
			case 2:
				var info = createRandomId();
				$('<div>').appendTo('#alert').attr('id', info).css("width",
						"100%").addClass('alert alert-info').html(message)
						.show().delay(time).fadeOut(function() {
							$("#" + info).remove();
						});
				break;
			case 3:
				var warning = createRandomId();
				$('<div>').appendTo('#alert').attr('id', warning).css("width",
						"100%").addClass('alert alert-warning').html(message)
						.show().delay(time).fadeOut(function() {
							$("#" + warning).remove();
						});
				break;
			case 4:
				var danger = createRandomId();
				$('<div>').appendTo('#alert').attr('id', danger).css("width",
						"100%").addClass('alert alert-danger').html(message)
						.show().delay(time).fadeOut(function() {
							$("#" + danger).remove();
						});
				break;
			}

			function createRandomId() {
				return (Math.random() * 100).toString(16).substr(0, 4) + '-'
						+ (new Date()).getTime() + '-'
						+ Math.random().toString().substr(2, 5);
			}
		}
	</script>
</body>
</html>